package com.webserver.controllers;

import com.webserver.http.HttpRequest;
import com.webserver.http.HttpResponse;
import com.webserver.vo.User;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

/**
 * 处理用户相关业务
 */
public class UserController {
    private static String usersDir = "./users";
    static {
        File userDir = new File(usersDir);
        if(!userDir.exists()){
            userDir.mkdirs();

        }
    }
    public void reg(HttpRequest request, HttpResponse response){
        System.out.println("开始处理用户注册!!!!!!!!!!!!");
        //1.通过request获取用户表单提交的数据
        //获取reg.html 中用户名这个输入框的值
        //这个request.getParameter()参数应当与页面上对应输入框的name属性值
        String username = request.getParameter("useame");
        String password = request.getParameter("password");
        String nickname = request.getParameter("nickname");
        String ageStr = request.getParameter("age");
        int age = Integer.parseInt(ageStr);
        //2.将该用户写入文件
        try(
                FileOutputStream fos = new FileOutputStream(
                        usersDir+username+".obj"
                        );
                ObjectOutputStream oos = new ObjectOutputStream(fos);
                ){
            User user = new User(username,password,nickname,age);
            oos.writeObject(user);

        //3.设置response响应登录结果页面
            File file = new File("./webapps/myweb/reg_success.html");
            response.setEntily(file);
            }catch (IOException e){
                e.printStackTrace();
            }
    }
}
